﻿using Direct.IDAL.Manager.Money;
using Direct.IDAL.User.Money;
using DirectSell.Common.Filter;
using DirectSell.Common.Page;
using DirectSell.IBLL;
using DirectSell.Model.Manager;
using DirectSell.Model.Money;
using DirectSell.Model.Query;
using DirectSell.Model.User;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DirectSell.WebLayer.Areas.Manager.Controllers
{
    /// <summary>
    /// 电子币审核
    /// </summary>
    public class VirtualMoneyAuditController : Controller
    {
        #region 注入
        [Dependency("ManagerVirtualMoney")]
        protected IManagerOperation _service { get; set; }

        [Dependency]
        protected IVirtualMoneyAuditRepository _repository { get; set; }

        [Dependency]
        protected IVirtualMoneyRepository _userrepository { get; set; }

        #endregion

        #region 首页
        /// <summary>
        /// 首页
        /// </summary>
        /// <returns></returns>
        [ManagerFilter(IsCheck =true)]
        public async Task<ActionResult> Index(BaseCondition condition)
        {
            condition.startTime = Request["startTime"];

            condition.endTime = Request["endTime"];

            var list = await _repository.GetRecord(condition);

            if (Request.IsAjaxRequest())
                return PartialView("_AjaxSearchPost", list);

            return View(list);
        }
        #endregion

        #region 审核
        /// <summary>
        /// 审核
        /// </summary>
        /// <param name="req"></param>
        /// <returns></returns>
        public async  Task<ManagerRespCode> Audit(VirtualMoneyAudit req)
        {
            return await _service.DoOperationAsync(req);
        }
        #endregion

        #region 增减电子币页面
        /// <summary>
        /// 增减电子币
        /// </summary>
        /// <returns></returns>
        public ActionResult MoneyIndex()
        {
            return PartialView();
        }
        #endregion

        #region 增减电子币
        /// <summary>
        /// 增减电子币
        /// </summary>
        /// <param name="usercode"></param>
        /// <returns></returns>
        public JsonResult Allowance(VirtualMoneyReq req)
        {
            var info = _repository.GetUserInfo(req.UserCode);

            if (info == null)
            {
                return Json("会员不存在");
            }

            req.UserName = info.UserName;

            if (!_userrepository.Apply(req))
            {
                return Json("操作失败");
            }

            return Json("操作成功,请审核");
        }

        #endregion
    }
}